package model

import (
	"gorm.io/gorm"
	"shorturl/global"
)

//CREATE TABLE `s_link` (
//`id` int NOT NULL AUTO_INCREMENT COMMENT '自增ID',
//`no_id` int DEFAULT NULL COMMENT '数字ID',
//`hash_id` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '短hash链接id',
//`cust_name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '自定义别名',
//`long_url` text COLLATE utf8mb4_general_ci COMMENT '原始链接',
//`click` int DEFAULT '0' COMMENT '点击数',
//`state` tinyint DEFAULT '0' COMMENT '0未启用 1已启用',
//`last_access_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上次访问时间',
//`create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
//`update_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
//`delete_at` timestamp NULL DEFAULT NULL COMMENT '删除时间',
//PRIMARY KEY (`id`)
//) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

type Link struct {
	Id           int            `json:"id" gorm:"column:id;type:int(11);not null;primaryKey;autoIncrement"`
	NoId         int            `json:"no_id" gorm:"column:no_id;type:int(11);default:null"`
	HashId       string         `json:"hash_id" gorm:"column:hash_id;type:varchar(10);default:null"`
	CustName     string         `json:"cust_name" gorm:"column:cust_name;type:varchar(255);default:null"`
	LongUrl      string         `json:"long_url" gorm:"column:long_url;type:text"`
	Click        int            `json:"click" gorm:"column:click;type:int(11);default:0"`
	State        int            `json:"state" gorm:"column:state;type:tinyint(1);default:null"`
	LastAccessAt LocalTime      `json:"last_access_at" gorm:"column:last_access_at;type:timestamp"`
	CreateAt     LocalTime      `json:"create_at" gorm:"column:create_at;type:timestamp"`
	UpdateAt     LocalTime      `json:"update_at" gorm:"column:update_at;type:timestamp"`
	DeleteAt     gorm.DeletedAt `json:"delete_at" gorm:"column:delete_at;type:timestamp"`
}

func (link Link) GetLinkById(id int) (*Link, error) {
	var linkInfo Link
	err := global.Db.Where("id = ?", id).First(&linkInfo).Error
	if err != nil {
		return nil, err
	}
	return &linkInfo, nil
}
